<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author Quality
 * @see 质量监督管理
 */
class QualityController extends CController {
	/**
	 *
	 * @see 质量监督管理-质量监督列表
	 */
	function actionList() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$areaList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 2,
				'deleted' => 0 
		), "sys_order_num" );
		$this->render ( 'list', array (
				'area' => $areaList 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-竣工验收申请
	 */
	function actionApplication() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$areaList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 2,
				'deleted' => 0 
		), "sys_order_num" );
		$this->render ( 'application', array (
				'area' => $areaList 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-验收备案管理
	 */
	function actionRecord() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$areaList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 2,
				'deleted' => 0 
		), array (
				"sys_order_num" => 'desc' 
		) );
		$this->render ( 'record', array (
				'area' => $areaList 
		) );
	}
	
	/**
	 *
	 * @see ajax 项目监督审核并立项
	 *     
	 */
	function actionAjax_quality_initiating_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$service = new QualityEntity ();
		$pg = $_GET ['page'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		
		$where = array (
				'deleted' => 0,
				'status' => 1 
		);
		
		$name = $_GET ['pname'] or "";
		// 工程名称
		if ($name != "") {
			$where ['name'] = "[code] like '%$name%'[code]";
		}
		$tb = $_GET ['tb'];
		switch ($tb) {
			default :
				break;
			case "1" :
				$where ['audit'] = "1";
				// 立项审核通过
				break;
			case "2" :
				$where ['audit'] = "2";
			// 立项审核不通过
		}
		sys_debug_log ( $where );
		$totalCount = $service->get_record_count ( $where );
		$tc_where = $where;
		unset ( $tc_where ['audit'] );
		$pass_where = $where;
		$pass_where ['audit'] = "1";
		$unpass_where = $where;
		$unpass_where ['audit'] = "2";
		$ret->tct = $service->get_record_count ( $tc_where );
		$ret->psct = $service->get_record_count ( $pass_where );
		$ret->upsct = $service->get_record_count ( $unpass_where );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$dicService = new SysDictionaryEntity ();
			$areaList = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 2,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$nature = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 3,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$status = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 4,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$index = 0;
			foreach ( $list as $item ) {
				$list [$index] ['address_str'] = array_seach_multi ( $areaList, 'id', $item ['address'], 'sys_value' );
				$list [$index] ['nature_str'] = array_seach_multi ( $nature, "id", $item ['nature'], "sys_value" );
				$list [$index] ['status_str'] = array_seach_multi ( $status, 'id', $item ['status'], 'sys_value' );
				$index ++;
			}
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax 工程质量监督
	 *     
	 */
	function actionAjax_quality_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$service = new QualityEntity ();
		$pg = $_GET ['page'];
		$where = array (
				'deleted' => 0,
				'status' => 1,
				'audit' => 1 
		);
		$name = $_GET ['pname'] or "";
		// 工程名称
		if ($name != "") {
			$where ['name'] = "[code] like '%$name%'[code]";
		}
		$tb = $_GET ['tb'];
		switch ($tb) {
			default :
				break;
			case "1" :
				$where ['status'] = "16";
				// 立项审核通过
				break;
			case "2" :
				$where ['status'] = "17";
			// 立项审核不通过
		}
		sys_debug_log ( $where );
		
		$totalCount = $service->get_record_count ( $where );
		$tc_where = $where;
		$tc_where ['status'] = array (
				'30',
				'16',
				'17' 
		);
		$sltz_where = $where;
		$sltz_where ['status'] = "16";
		$zljd_where = $where;
		$zljd_where ['status'] = "17";
		$ret->tct = $service->get_record_count ( $tc_where );
		$ret->sltzct = $service->get_record_count ( $sltz_where );
		$ret->zljdct = $service->get_record_count ( $zljd_where );
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$dicService = new SysDictionaryEntity ();
			$areaList = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 2,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$nature = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 3,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$index = 0;
			foreach ( $list as $item ) {
				$list [$index] ['address_str'] = array_seach_multi ( $areaList, 'id', $item ['address'], 'sys_value' );
				$list [$index] ['nature_str'] = array_seach_multi ( $nature, "id", $item ['nature'], "sys_value" );
				// $list [$index] ['status_str'] = array_seach_multi ( $status, 'id', $item ['status'], 'sys_value' );
				$index ++;
			}
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax 项目监督审核并立项
	 *     
	 */
	function actionAjax_quality_application_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$service = new QualityEntity ();
		
		$pg = $_GET ['page'];
		$where = array (
				'deleted' => 0,
				'status' => 2 
		);
		$name = $_GET ['pname'] or "";
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
			
			// 工程名称
		if ($name != "") {
			$where ['name'] = "[code] like '%$name%'[code]";
		}
		$tb = $_GET ['tb'];
		switch ($tb) {
			default :
				break;
			case "1" :
				$where ['audit'] = "1";
				// 立项审核通过
				break;
			case "2" :
				$where ['audit'] = "2";
			// 立项审核不通过
		}
		sys_debug_log ( $where );
		
		//
		$tc_where = $where;
		unset ( $tc_where ['audit'] );
		$pass_where = $where;
		$pass_where ['audit'] = "1";
		$unpass_where = $where;
		$unpass_where ['audit'] = "2";
		$ret->tct = $service->get_record_count ( $tc_where );
		$ret->psct = $service->get_record_count ( $pass_where );
		$ret->upsct = $service->get_record_count ( $unpass_where );
		
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$dicService = new SysDictionaryEntity ();
			$areaList = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 2,
					'deleted' => 0 
			), "sys_order_num" );
			$nature = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 3,
					'deleted' => 0 
			), "sys_order_num" );
			$status = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 4,
					'deleted' => 0 
			), "sys_order_num" );
			$index = 0;
			foreach ( $list as $item ) {
				$list [$index] ['address_str'] = array_seach_multi ( $areaList, 'id', $item ['address'], 'sys_value' );
				$list [$index] ['nature_str'] = array_seach_multi ( $nature, "id", $item ['nature'], "sys_value" );
				$list [$index] ['status_str'] = array_seach_multi ( $status, 'id', $item ['status'], 'sys_value' );
				$index ++;
			}
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax 项目监督审核并立项
	 *     
	 */
	function actionAjax_quality_record_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$service = new QualityEntity ();
		$pg = $_GET ['page'];
		
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$where = array (
				'deleted' => 0,
				'audit' => 1,
				'status' => 2 
		);
		
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		
		//
		$tc_where = $where;
		$tc_where ['status'] = array (
				'20',
				'19',
				'32' 
		);
		
		$jgys_where = $where;
		$jgys_where ['status'] = "19";
		$jgba_where = $where;
		$jgba_where ['status'] = "20";
		$ret->tct = $service->get_record_count ( $tc_where );
		$ret->jgysct = $service->get_record_count ( $jgys_where );
		$ret->jgbact = $service->get_record_count ( $jgba_where );
		
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$dicService = new SysDictionaryEntity ();
			$areaList = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 2,
					'deleted' => 0 
			), "sys_order_num" );
			$nature = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 3,
					'deleted' => 0 
			), "sys_order_num" );
			$status = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 4,
					'deleted' => 0 
			), "sys_order_num" );
			$index = 0;
			foreach ( $list as $item ) {
				$list [$index] ['address_str'] = array_seach_multi ( $areaList, 'id', $item ['address'], 'sys_value' );
				$list [$index] ['nature_str'] = array_seach_multi ( $nature, "id", $item ['nature'], "sys_value" );
				$list [$index] ['status_str'] = array_seach_multi ( $status, 'id', $item ['status'], 'sys_value' );
				$index ++;
			}
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-增加
	 */
	function actionAdd() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$areaList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 2,
				'deleted' => 0 
		), "sys_order_num" );
		$nature = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 3,
				'deleted' => 0 
		), "sys_order_num" );
		$status = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 4,
				'deleted' => 0 
		), "sys_order_num" );
		$this->render ( 'add', array (
				'area' => $areaList,
				'nature' => $nature,
				'status' => $status 
		) );
	}
	function actionAjax_quality_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		$name = $_POST ['name'];
		// 项目名称
		$nature = $_POST ['nature'];
		// 工程性质
		$address = $_POST ['address'];
		// 地址
		$structure_type = $_POST ['structure_type'];
		// 结构层次类型/层次
		$area = $_POST ['area'];
		// 面积
		$cost = $_POST ['cost'];
		// 工程造价
		$construction_unit = $_POST ['construction_unit'];
		// 施工单位
		$reconnaissance_unit = $_POST ['reconnaissance_unit'];
		// 勘察单位
		$supervision_units_name = $_POST ['supervision_units_name'];
		// 监理单位
		$status = $_POST ['status'];
		$fld = array (
				'name' => $name,
				'nature' => $nature,
				'address' => $address,
				'structure_type' => $structure_type,
				'area' => $area,
				'cost' => $cost,
				'construction_unit' => $construction_unit,
				'reconnaissance_unit' => $reconnaissance_unit,
				'supervision_units_name' => $supervision_units_name,
				'status' => $status 
		);
		$service = new QualityEntity ();
		$result = $service->add ( $fld );
		if (is_numeric ( $result ) && $result > 0) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-受理通知书
	 */
	function actionNotice() {
		$id = $_GET ['id'];
		$service = new QualityEntity ();
		$fld = array (
				'name',
				'(SELECT company_name from user where id = company_id LIMIT 1) as company_name' 
		);
		$model = $service->get ( $id, $fld );
		sys_debug_log ( $model );
		$this->render ( 'notice', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-监督记录列表
	 */
	function actionSupervision() {
		$id = $_GET ['id'];
		$this->render ( 'supervision', array (
				'quality_id' => $id 
		) );
	}
	/**
	 *
	 * @see 监督记录列表
	 */
	function actionAjax_supervision_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$service = new SupervisionRecordEntity ();
		$pg = $_GET ['page'];
		$id = $_GET ['quality_id'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$where = array (
				'quality_id' => $id,
				'deleted' => 0 
		);
		
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			
			$ret->status = 200;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-监督记录列表-新增
	 */
	function actionSupervisionAdd() {
		$qid = $_GET ['quality_id'];
		$id = $_GET ['id'];
		sys_debug_log ( is_numeric ( $qid ) && $qid > 0 );
		if (is_numeric ( $id ) && $id > 0) {
			$service = new SupervisionRecordEntity ();
			$model = $service->get ( $id );
			if ($model === false || count ( $model ) === 0) {
				exit ( "已超时，请刷新页面重新操作" );
			}
			$this->render ( 'supervisionAdd', array (
					'model' => $model 
			) );
		} else if (is_numeric ( $qid ) && $qid > 0) {
			
			$this->render ( 'supervisionAdd', array (
					'quality_id' => $qid 
			) );
		} else {
			exit ( "已超时，请刷新页面重新操作" );
		}
	}
	
	/**
	 *
	 * @see ajax 编辑监督记录信息
	 */
	function actionAjax_supervision_editor() {
		$ret = new stdClass ();
		$ret->status = 500;
		$id = $_POST ['id'];
		$quality_id = $_POST ['quality_id'];
		$su_title = $_POST ['su_title'];
		// 类型
		$category = $_POST ['category'];
		// 内容
		$su_content = $_POST ['su_content'];
		// 职位
		$service = new SupervisionRecordEntity ();
		if (is_numeric ( $id ) && $id > 0) {
			$model = $service->get ( $id );
			if (false === $model) {
				$ret->status = 500;
				$ret->data = "参数错误";
				echo json_encode ( $ret );
				return;
			}
			$fld = array (
					'su_title' => $su_title,
					'category' => $category,
					'su_content' => $su_content 
			);
			$result = $service->update ( $fld, array (
					'id' => $id 
			) );
			if (false === $result) {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			}
		} else {
			$fld = array (
					'su_title' => $su_title,
					'category' => $category,
					'su_content' => $su_content,
					'deleted' => 0,
					'add_time' => date ( 'Y-m-d H:i:s', time () ),
					'quality_id' => $quality_id 
			);
			$result = $service->insert ( $fld );
			if (is_numeric ( $result ) && $result > 0) {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			}
		}
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-监督记录列表-详情
	 */
	function actionSupervisionDetail() {
		$id = $_GET ['id'];
		$service = new SupervisionRecordEntity ();
		$model = $service->get ( $id );
		if ($model === false || count ( $model ) === 0) {
			exit ( "已超时，请刷新页面重新操作" );
		}
		$this->render ( 'supervisionDetail', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-整改通知
	 */
	function actionRectification() {
		$this->render ( 'rectification' );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-验收申请
	 */
	function actionApply() {
		$this->render ( 'apply' );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-实施验收
	 */
	function actionAcceptance() {
		$this->render ( 'acceptance' );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-质量监督报告
	 */
	function actionQuality() {
		$this->render ( 'quality' );
	}
	
	/**
	 *
	 * @see 质量监督管理-质量监督列表-竣工备案
	 */
	function actionCompletion() {
		$this->render ( 'completion' );
	}
	
	/**
	 *
	 * @see 质量监督科-审核立项项目通知书
	 */
	function actionInitiating() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$areaList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 2,
				'deleted' => 0 
		), "sys_order_num" );
		$this->render ( 'initiating', array (
				'area' => $areaList 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-审核竣工申请通知书
	 */
	function actionapplicationNotice() {
		$id = $_GET ['id'];
		$service = new QualityEntity ();
		$model = $service->get ( $id );
		if ($model === false) {
			exit ( "超时，请刷新网页重新操作" );
		}
		$cmService = new UserEntity ();
		
		$cmModel = $cmService->get ( $model ['company_id'], array (
				'company_name' 
		) );
		
		$model ['company_name'] = $cmModel ['company_name'];
		$this->render ( 'applicationNotice', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-项目监督审核立项详情
	 */
	function actioninitiatingDetail() {
		$id = $_GET ['id'];
		$service = new QualityEntity ();
		$model = $service->get ( $id );
		$this->render ( 'initiatingDetail', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 质量监督管理-审核竣工申请详情
	 */
	function actionapplicationDetail() {
		$id = $_GET ['id'];
		$service = new QualityEntity ();
		$model = $service->get ( $id );
		$this->render ( 'applicationDetail', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 质量监督审核立项
	 */
	function actionAjax_initating_status() {
		$ret = new stdClass ();
		$ret->status = 401;
		$ty = $_POST ['ty'];
		$id = $_POST ['id'];
		$status = "0";
		if ($ty == "1") {
			$status = "1";
		} else if ($ty == "2") {
			$status = "2";
		} else {
			$ret->data = "操作失败";
			echo json_encode ( $ret );
			return;
		}
		$service = new QualityEntity ();
		$result = $service->update ( array (
				'audit' => $status 
		), array (
				'id' => $id 
		) );
		if ($result === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
}
